
Create PROC [dbo].[sp_KhoHang_Insert](
	@MaSanPham				Int,
	@SoLuong				INT
)
AS
BEGIN
	DECLARE @Ngay_cap_nhat_cuoi datetime,@soluong int,
	SET @Ngay_cap_nhat_cuoi = getdate()

	
if not exists (select * from khohang where ma_san_pham = @MaSanPham)
		Begin
		INSERT INTO KhoHang(	
							ma_san_pham,
							soluong,
							ngay_cap_nhat_cuoi
							) 
					VALUES(	
							@MaSanPham,
							@SoLuong,
							@Ngay_cap_nhat_cuoi
							)
		End
	else
		Begin
			declare @tongso int
			set @tongso = (select sum(soluong) from khohang where ma_san_pham=@MaSanPham ) + @soluong
			
			update khohang set soluong=@tongso where ma_san_pham=@MaSanPham
		End
END
